package com.itcast.mq.listen;

import com.itcast.mq.config.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;

/**
 * @Author:王亚栋
 * @Date: 2023/6/6 下午8:51
 * @Version:1.0
 */
@Component
@Slf4j(topic = "SpringRabbitmqListener")
public class SpringRabbitmqListener {

//    @RabbitListener(queues = Constant.queueName)
//    public void listenSimpleQueueMessage(String msg) throws InterruptedException{
//
//        log.info("Spring 消费者收到的消息：{}",msg);
//
//    }

    @RabbitListener(queues = Constant.queueName)
    public void listenWork1QueueMessage(String msg) throws InterruptedException {

        log.info("消费者1收到的消息：{}",msg);

        Thread.sleep(200);

    }

    @RabbitListener(queues = Constant.queueName)
    public void listenWork2QueueMessage(String msg) throws InterruptedException {

        log.info("消费者2收到的消息：{}",msg + LocalTime.now());

        Thread.sleep(20);
    }

    @RabbitListener(queues = Constant.fanoutQueueName1)
    public void listenFanoutQueue1(String msg) {

        log.info("消费者收到fanout.queue1的消息：{}",msg);


    }

    @RabbitListener(queues = Constant.fanoutQueueName2)
    public void listenFanoutQueue2(String msg) {

        log.info("消费者收到fanout.queue2的消息：{}",msg);


    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(String msg){

        log.info("消费者收到direct.queue1的消息：{}",msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void listenDirectQueue2(String msg){

        log.info("消费者收到direct.queue2的消息：{}",msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg){

        log.info("消费者收到topic.queue1的消息：{}",msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue2(String msg){

        log.info("消费者收到topic.queue2的消息：{}",msg);
    }
}
